package cn.cangfei.massage.service.impl;

import cn.cangfei.massage.dto.param.MassageClinicParam;
import cn.cangfei.massage.entity.MassageClinic;
import cn.cangfei.massage.entity.MassageClinicExample;
import cn.cangfei.massage.entity.MassageOutpatient;
import cn.cangfei.massage.mapper.MassageClinicMapper;
import cn.cangfei.massage.service.IMassageClinicService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Lemon  2313812755@qq.com
 * @date 2023/2/12
 */
@Service
public class MassageClinicServiceImpl implements IMassageClinicService {

    @Resource
    private MassageClinicMapper clinicMapper;

    /**
     * 插入诊室信息
     *
     * @param param 诊室信息参数
     * @return 是否成功
     */
    @Override
    public boolean insert(MassageClinicParam param) {

        MassageClinic clinic = new MassageClinic();

        BeanUtils.copyProperties(param, clinic);

        clinic.setGmtCreate(new Date());
        clinic.setGmtModified(new Date());

        return clinicMapper.insertSelective(clinic) > 0;
    }

    /**
     * 更新诊室信息
     *
     * @param id    诊室编号
     * @param param 诊室信息参数
     * @return 是否成功
     */
    @Override
    public boolean update(Long id, MassageClinicParam param) {
        MassageClinic clinic = new MassageClinic();

        BeanUtils.copyProperties(param, clinic);

        clinic.setId(id);
        clinic.setGmtModified(new Date());

        return clinicMapper.updateByPrimaryKeySelective(clinic) > 0;
    }

    /**
     * 获取诊室名称
     *
     * @param id 诊室编号
     * @return 诊室地址，空则返回，未知
     */
    @Override
    public String getAddress(Long id) {
        return getOptional(id).map(MassageClinic::getAddress).orElse("未知");
    }

    /**
     * 获取诊室信息
     *
     * @param id 诊室编号
     * @return 诊室信息
     */
    @Override
    public Optional<MassageClinic> getOptional(Long id) {
        return Optional.ofNullable(clinicMapper.selectByPrimaryKey(id));
    }

    /**
     * 删除诊室信息
     *
     * @param id 诊室编号
     * @return 是否成功
     */
    @Override
    public boolean delete(Long id) {
        return clinicMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 获取门诊下属的诊室
     *
     * @param outpatientId 门诊编号
     * @param pageNum      第几页
     * @param pageSize     页大小
     * @return 诊室列表
     */
    @Override
    public List<MassageClinic> list(Long outpatientId, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        MassageClinicExample example = new MassageClinicExample();

        example.createCriteria()
                .andOutpatientIdEqualTo(outpatientId);

        return clinicMapper.selectByExample(example);
    }

    /**
     * 根据outPatients获取到全部的clinicId
     *
     * @param outpatients
     * @return
     */
    @Override
    public List<MassageClinic> getClinicIdsByOutPatients(List<MassageOutpatient> outpatients) {
        List<Long> outpatientsIds = outpatients.stream().map(MassageOutpatient::getId).collect(Collectors.toList());


        MassageClinicExample massageClinicExample = new MassageClinicExample();
        massageClinicExample.createCriteria()
                .andOutpatientIdIn(outpatientsIds);

        List<MassageClinic> massageClinics = clinicMapper.selectByExample(massageClinicExample);
        return massageClinics;
    }

    /**
     * 判断诊室是否存在
     *
     * @param id 诊室编号
     * @return 是否存在
     */
    @Override
    public boolean count(Long id) {
        MassageClinicExample example = new MassageClinicExample();

        example.createCriteria()
                .andIdEqualTo(id);

        return clinicMapper.countByExample(example) > 0;
    }
}
